package com.klun.concDesign.chapterThree;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 1.accept,thanks god,where are you living......2%
 * 2.implicit refuse,ok? 88%
 * 3.direct refuse, what should I do? 10%
 */
public class ReenterLockCondition implements Runnable {
    public static ReentrantLock lock = new ReentrantLock();
    public static Condition condition = lock.newCondition();

    @Override
    public void run() {
        try {
            lock.lock();
            // await() : 会使当前线程等待，同时释放当前锁，当其他线程使用signal()或者signalAll()时 获得锁继续执行
            condition.await();
            System.out.println("Thread is going on");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("线程解锁");
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReenterLockCondition r1 = new ReenterLockCondition();
        Thread t1 = new Thread(r1);
        t1.start();
        Thread.sleep(10000);
        // 给通知信号
        lock.lock();
        condition.signal();
        lock.unlock();
    }

}
